<script>
	import config from 'config.js'
	import util from 'utils/util.js'
    import store from '@/store'
    import { getToken } from '@/utils/auth'
  
	export default {
	
		onLaunch: function() {
			this.initApp()
			let info = uni.getSystemInfoSync()
			info.versionCode = 0
			info.version = config.info.version
			info.appVersion = config.info.version
			info.bookshelfChanged = false

			if (info.model && info.model.indexOf('iPhone') !== -1) {
				info.titleBarHeight = 44
			} else {
				info.titleBarHeight = 48
			}

			// 新版小程序升级检测
			// #ifdef MP-WEIXIN
			if (uni.canIUse('getUpdateManager')) {
				const updateManager = uni.getUpdateManager()
				updateManager.onCheckForUpdate(function(res) {
					if (res.hasUpdate) {
						updateManager.onUpdateReady(function() {
							uni.showModal({
								title: '升级提示',
								content: '新版本已经为您准备就绪，是否升级应用？',
								success: function(res) {
									if (res.confirm) updateManager.applyUpdate()
								}
							})
						})
					}
				})
			}
			// #endif

			// #ifdef APP-PLUS
			// 查询版本信息
			plus.runtime.getProperty(plus.runtime.appid, function(wgtInfo) {
				info.versionCode = wgtInfo.versionCode
				info.version = wgtInfo.version

				// 如果是Android平台，则检测是否有新版本
				if (String(info.platform).toLowerCase() == "android") {
					if (config.debug) console.log("request version api", config.api.lastestVersion)
					util.request(config.api.lastestVersion).then(function(res) {
						if (config.debug) console.log("latest version:", res)
						if (res && res.data && res.data.version) {
							if (config.debug) console.log("当前版本", info.versionCode, "最新版本", res.data.version)
							if (info.versionCode < res.data.version) {
								uni.showModal({
									title: "温馨提示",
									content: "发现新版本APP，您是否要升级体验？",
									cancelText: "暂时忽略",
									confirmText: "码上升级",
									success: (action) => {
										if (action.confirm) {
											if (config.debug) console.log('用户点击确定');
											plus.runtime.openURL(res.data.url)
										} else if (res.cancel) {
											if (config.debug) console.log('用户点击取消');
										}
									}
								})
							}
						}
					}).catch(function(e) {
						console.log(e)
					})
				}
			});
			// #endif

			if (config.debug) {
				console.log(info)
				console.log('platform', info.platform)
			}
			util.setSysInfo(info)
		},
		methods: {
		  // 初始化应用
		  initApp() {
		    // 初始化应用配置
		    this.initConfig()
		    // 检查用户登录状态
		    //#ifdef H5
		    this.checkLogin()
		    //#endif
		  },
		  initConfig() {
		    this.globalData.config = config
		  },
		  checkLogin() {
		    if (!getToken()) {
		      this.$tab.reLaunch('/pages/login') 
		    }
		  }
		},
		onShow: function() {
			if (config.debug) console.log('App Show')
		},
		onHide: function() {
			if (config.debug) console.log('App Hide')
		},
	}
</script>
<!-- <style>
	@import url("static/css/bookchat.css");
</style> -->

 
 <!-- 
 <script>
   import config from './config'
   import store from '@/store'
   import { getToken } from '@/utils/auth'
 
   export default {
     onLaunch: function() {
       this.initApp()
     },
     methods: {
       // 初始化应用
       initApp() {
         // 初始化应用配置
         this.initConfig()
         // 检查用户登录状态
         //#ifdef H5
         this.checkLogin()
         //#endif
       },
       initConfig() {
         this.globalData.config = config
       },
       checkLogin() {
         if (!getToken()) {
           this.$tab.reLaunch('/pages/login') 
         }
       }
     }
   }
 </script>
 
 <style lang="scss">
   @import '@/static/scss/index.scss'
 </style> -->
